/**
 * Created with IntelliJ IDEA.
 *
 * @author: cxs
 * @date: 2023/7/3
 * @time: 9:28
 * @className：RentServiceImpl
 * @description：
 */
package com.bailan.bus.service.impl;

import com.bailan.bus.domain.Car;
import com.bailan.bus.domain.Rent;
import com.bailan.bus.mapper.CarMapper;
import com.bailan.bus.mapper.RentMapper;
import com.bailan.bus.service.IRentService;
import com.bailan.bus.vo.CarVo;
import com.bailan.bus.vo.RentVo;
import com.bailan.common.util.DataGridView;
import com.bailan.common.util.SysConstant;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class RentServiceImpl implements IRentService {
    @Autowired
    RentMapper rentMapper;
    @Autowired
    CarMapper carMapper;
    /**
     * 添加出租信息
     *
     * @param rentVo
     */
    @Override
    public void addRent(RentVo rentVo) {
        this.rentMapper.insertSelective(rentVo);
        //更改车辆出租的状态
        Car car=new Car();
        car.setCarnumber(rentVo.getCarnumber());
        //设置状态为已出租
        car.setIsrenting(SysConstant.RENT_BACK_TRUE);
        carMapper.updateCarByCarName(car);
    }

    /**
     * 查询出租单信息
     *
     * @param rentVo
     * @return
     */
    @Override
    public DataGridView queryAllRent(RentVo rentVo) {
        Page<Object> page=PageHelper.startPage(rentVo.getPage(), rentVo.getLimit());
        List<Rent> data= this.rentMapper.queryAllRent(rentVo);
        return  new DataGridView(page.getTotal(),data);
    }

    /**
     * 编辑出租单
     *
     * @param rent
     */
    @Override
    public void updateRent(Rent rent) {
        this.rentMapper.updateRentByRentId(rent);
    }

    /**
     * 删除出租单信息
     * @param rentid
     */
    @Override
    public void deleteRent(String rentid) {
        //更改汽车状态
        Rent rent=this.rentMapper.selectByPrimaryKey(rentid);
        CarVo car=new CarVo();
        car.setCarnumber(rent.getCarnumber());
        car.setIsrenting(SysConstant.RENT_BACK_FALSE);
        carMapper.updateCarByCarName(car);
        this.rentMapper.deleteRent(rentid);
    }

    /**
     * 根据id查询出租单信息
     *
     * @param rentid
     * @return
     */
    @Override
    public Rent selectRentByRentId(String rentid) {
        return rentMapper.selectByPrimaryKey(rentid);
    }
}